//
//  AppendLogsableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/9.
//

#import "AppendLogsableViewCell.h"
#import "SYAudio.h"

@interface AppendLogsableViewCell ()
@property (nonatomic ,strong) UIView * backview;

@property (nonatomic,strong) NSTimer * timer;
@property (nonatomic, assign) int timeS;
@property (nonatomic ,strong) UIButton * playBtn;

@end
@implementation AppendLogsableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    self.contentView.clipsToBounds = YES;
    
    UIView * backview = [UIView new];
    backview.backgroundColor = UIColor.whiteColor;
    self.backview= backview;
    [DTKTool YJ:kwt(5) andView:backview];
    [self.contentView addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.bottom.offset(kwt(-10));
    }];
}

- (void)setModel:(AppendLogsModel *)model{
    _model = model;
    
    [self.backview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat leftY = 0;
    UIImageView * oldImg;
    ///左边画个标
    UIImageView * img = kimageView(@"complete");
    [DTKTool YJBK:kwt(9) andView:img andWidth:kwt(1.5) andColor:KcolorHex(@"#90B5FA")];
    [self.backview addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(65));
        make.width.height.offset(kwt(18));
        make.top.offset(kwt(20));
    }];
    UIView * line = [UIView new];
    line.backgroundColor= KcolorHex(@"#D0D0D0");
    [self.backview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(img);
        make.width.offset(1);
        make.top.equalTo(img.mas_bottom).offset(kwt(3));
        make.bottom.offset(kwt(-10));
    }];
    
    ///时间标题
    
    UILabel * bottomtime = [DTKTool labelWithText:[model.appTime componentsSeparatedByString:@" "].lastObject andFont:kCFont(16) andTextColor:KcolorHex(@"#1C1C1C")];
    [self.backview addSubview:bottomtime];
    [bottomtime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(30));
    }];
    
    
    UILabel * toptime = [DTKTool labelWithText:[model.appTime componentsSeparatedByString:@" "].firstObject andFont:kCFont(14) andTextColor:KcolorHex(@"#989898")];
    [self.backview addSubview:toptime];
    [toptime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomtime);
        make.top.offset(kwt(13));
    }];
    
    if(model.batch){///批量打卡
        UILabel * batchFlag = [DTKTool labelWithText:@"批量打卡" andFont:kCFont(12) andTextColor:UIColor.whiteColor];
        [self.backview addSubview:batchFlag];
        batchFlag.textAlignment = NSTextAlignmentCenter;
        batchFlag.backgroundColor  = KcolorHex(@"#29B560");
        [DTKTool BFYJ:kwt(8) andView:batchFlag andCorner:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft andSize:CGRectMake(0, 0,kwt(54), kwt(22))];
        [batchFlag mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(bottomtime);
            make.top.offset(kwt(55));
            make.width.offset(kwt(54));
            make.height.offset(kwt(22));
        }];
        
    }
    
    UILabel * name = [DTKTool labelWithText:model.tissueListStr andFont:kCFont(16) andTextColor:KcolorHex(@"#222222")];
    [self.backview addSubview:name];
    name.numberOfLines = 0;
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(95));
        make.width.offset(kwt(265));
        make.top.offset(kwt(20));
        make.height.offset(model.tissueListStrH);
    }];
  
    UIView * tempview = [UIView new];
    tempview.backgroundColor = KcolorHex(@"#0967FF");
    [DTKTool YJ:kwt(4) andView:tempview];
    [self.backview addSubview:tempview];
    [tempview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).offset(kwt(11));
        make.left.offset(kwt(95));
        make.width.height.offset(kwt(8));
    }];
    ///任务标题
    UILabel * temp = [DTKTool labelWithText:model.taskName andFont:kCFont(15) andTextColor:KcolorHex(@"#0967FF")];
    [self.backview addSubview:temp];
    [temp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tempview);
        make.left.equalTo(tempview.mas_right).offset(kwt(8));
    }];
    
    UIView * backview = [UIView new];
//    backview.backgroundColor = KcolorHex(@"#F7F7F9");
    [DTKTool YJ:kwt(5) andView:backview];
    [self.backview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tempview.mas_bottom).offset(kwt(11));
        make.left.offset(kwt(95));
        make.right.offset(kwt(-10));
        make.bottom.offset(kwt(-10));
    }];
    //
    CGFloat tempY = 0;

    CGFloat btnH = 0;
    if( model.taskDetailModelList.count > 1){
        btnH = Kwt(40);
    }
    ///打卡任务
    for (taskDetailModelListModel * listmodel in model.taskDetailModelList) {///其实就是鱼塘数据
        UIView * tempview = [UIView new];
        tempview.backgroundColor = UIColor.whiteColor;
        
        if(listmodel.isSQ){
            tempview.frame = CGRectMake(0, tempY, kwt(268),  kwt(40));
        } else {
            tempview.frame = CGRectMake(0, tempY, kwt(268), listmodel.strH + kwt(16) + btnH);
        }
        [backview addSubview:tempview];
        //                 创建CAShapeLayer用于绘制虚线边框
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.frame = tempview.bounds;
        // 使用UIBezierPath创建圆角矩形路径
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:tempview.bounds cornerRadius:kwt(3)];
        // 设置形状层的路径
        shapeLayer.path = path.CGPath;
        // 设置虚线样式
        shapeLayer.strokeColor = KcolorHex(@"#C2C2C2").CGColor; // 边框颜色
        shapeLayer.lineWidth = 0.5; // 边框宽度
        shapeLayer.lineDashPattern = @[@3, @3]; // 定义虚线模式，这里是4个单位的线，接着4个单位的空
        // 设置填充颜色为空，因为我们只需要边框
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        // 添加到视图的layer上
        [tempview.layer addSublayer:shapeLayer];
        
        if(btnH > 0){
            
            UILabel * pondlab = [DTKTool labelWithText:listmodel.name andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
            [tempview addSubview:pondlab];
            [pondlab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.offset(kwt(13));
                make.left.offset(kwt(8));
                make.height.offset(kwt(14));
            }];
            gywbtn * sxBtn = [gywbtn buttonWithType:0];
            sxBtn.infoDict = @{@"model":listmodel};
            sxBtn.addTarget(self,@selector(openSXBtnClick:));
            sxBtn.selected = listmodel.isSQ;
            [sxBtn setImage:[UIImage imageNamed:@"template_expand"] forState:UIControlStateNormal];
            [sxBtn setImage:[UIImage imageNamed:@"template_retract"] forState:UIControlStateSelected];
            [tempview addSubview:sxBtn];
            [sxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(pondlab);
                make.width.height.offset(kwt(20));
                make.right.offset(kwt(-14));
            }];
        }
        
        if(listmodel.isSQ == YES && btnH > 0){
            tempY += kwt(8)  + btnH;
        } else {
            UILabel * lab = [UILabel new];
            lab.numberOfLines = 0;
            lab.textAlignment = NSTextAlignmentLeft;
            lab.attributedText = listmodel.str;
            [tempview addSubview:lab];
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.offset(kwt(8) + btnH);
                make.left.offset(kwt(8));
                make.width.offset(kwt(252 ));
                make.height.offset(listmodel.RZstrH);
            }];
            tempY += kwt(8 + 20) + listmodel.RZstrH + btnH;
            
        }
        
    }

    MJWeakSelf
    if(model.cardImgArray.count > 0){
        UILabel * name = [DTKTool labelWithText:@"打卡照片" andFont:kCFont(13) andTextColor:KcolorHex(@"#222222")];
        [backview addSubview:name];
        [name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(tempY);
            make.left.offset(0);
        }];
        tempY += kwt(20);
        CGFloat X = 0;
        int i = 0;
        for (NSString * phone in model.cardImgArray) {
            UIImageView * img = [UIImageView new];
            [DTKTool YJ:kwt(3) andView:img];
            img.addClickBlock(^{
                [weakSelf openTPwithArray:model.cardImgArray andview:img andIndex:i];
            });
            [img LZsetImageWithImagePath:phone placeholderImage:[UIImage imageNamed:@"tangkou_photo"]];
            [backview addSubview:img];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.offset(tempY);
                make.left.offset(X);
                make.width.height.offset(kwt(52));
            }];
            X += kwt(60);
            if(X >= kwt(200)){
                X = 0;
                tempY += kwt(60);
            }
            i++;
        }
        tempY += kwt(60);
    }
    
    UIView * tempview1 = [UIView new];
    tempview1.backgroundColor = KcolorHex(@"#0967FF");
    [DTKTool YJ:kwt(4) andView:tempview1];
    [backview addSubview:tempview1];
    [tempview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(tempY + kwt(11));
        make.left.offset(0);
        make.width.height.offset(kwt(8));
    }];
    ///任务标题
    UILabel * zxrlab = [DTKTool labelWithText:[NSString stringWithFormat:@"执行人：%@",model.executor] andFont:kCFont(15) andTextColor:KcolorHex(@"#0967FF")];
    [backview addSubview:zxrlab];
    zxrlab.numberOfLines = 0;
    [zxrlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tempview1.mas_top).offset(kwt(-5));
        make.left.equalTo(tempview1.mas_right).offset(kwt(8));
        make.width.offset(kwt(250));
    }];
    tempY += kwt(10) + model.executorStrH;

    
    if(model.voiceUrl.length > 0){
        UIView * llview = [UIView new];
        llview.frame = CGRectMake(0, tempY, kwt(268), kwt(40));
        [backview addSubview:llview];
        //                 创建CAShapeLayer用于绘制虚线边框
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.frame = llview.bounds;
        
        // 使用UIBezierPath创建圆角矩形路径
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:llview.bounds cornerRadius:kwt(3)];
        
        // 设置形状层的路径
        shapeLayer.path = path.CGPath;
        
        // 设置虚线样式
        shapeLayer.strokeColor = KcolorHex(@"#C2C2C2").CGColor; // 边框颜色
        shapeLayer.lineWidth = 0.5; // 边框宽度
        shapeLayer.lineDashPattern = @[@3, @3]; // 定义虚线模式，这里是4个单位的线，接着4个单位的空
        
        // 设置填充颜色为空，因为我们只需要边框
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        
        // 添加到视图的layer上
        [llview.layer addSublayer:shapeLayer];
        
        UILabel * name = [DTKTool labelWithText:@"备注：" andFont:kCFont(14) andTextColor:KcolorHex(@"#222222")];
        [llview addSubview:name];
        [name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt(15));
        }];
        UIButton * playBtn = [UIButton new];
        self.playBtn = playBtn;
        [playBtn setImage:[UIImage imageNamed:@"language_playback0"] forState:UIControlStateNormal];
        [playBtn setImageEdgeInsets:UIEdgeInsetsMake(0, kwt(35), 0, kwt(-30))];
        [playBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, kwt(-20), 0, kwt(20))];
        playBtn.backgroundColor = KcolorHex(@"#29B560");
        [self addSubview:playBtn];
        playBtn.addClickBlock(^{
            [[SYAudio shareAudio] audioPlayWithURL:model.voiceUrl andTimeBlock:^(int time) {
                [weakSelf opentimeWithTimes:time];
            }];
        });
        if(model.voiceDuration.length > 0){
            [playBtn setTitle:[NSString stringWithFormat:@"%@",model.voiceDuration] forState:UIControlStateNormal];
        } else  {
            [playBtn setTitle:[NSString stringWithFormat:@"0\""] forState:UIControlStateNormal];
        }
        
        //                playBtn.addTarget(self,@selector(openplay));
        model.openDurationBlock = ^(NSString * _Nonnull voiceDuration) {
            weakSelf.model = weakSelf.model;
        };
        [DTKTool YJ:kwt(5) andView:playBtn];
        [llview addSubview:playBtn];
        [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(kwt(71));
            make.width.offset(kwt(90));
            make.height.offset(kwt(30));
            
        }];
    }
    
    //    [self.CLview mas_updateConstraints:^(MASConstraintMaker *make) {
    //        if(model.GJviewH > 0){
    //            make.top.equalTo(self.GJview.mas_bottom).offset(kwt(10));
    //        } else {
    //            make.top.offset(0);
    //        }
    //        make.height.offset(model.CLviewH);
    //    }];
    if(self.timer.isValid){
        [_timer invalidate];
    }
    _timer=nil;
    [self.playBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"language_playback0"]] forState:UIControlStateNormal];

}

- (void)opentimeWithTimes:(int)times{
    if(self.timer.isValid){
        [_timer invalidate];
    }
    _timer=nil;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1/3.0 target:self selector:@selector(openAudio) userInfo:nil repeats:YES];
    self.timeS = times * 3;
}
- (void)openAudio{
    self.timeS --;
    [self.playBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"language_playback%d",self.timeS%3]] forState:UIControlStateNormal];
    if(self.timeS < 0){
        if(self.timer.isValid){
            [_timer invalidate];
        }
        _timer=nil;
        [self.playBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"language_playback0"]] forState:UIControlStateNormal];
    }
}


- (void)openSXBtnClick:(gywbtn *)btn{
    taskDetailModelListModel * model = [btn.infoDict objectForKey:@"model"];
    model.isSQ = !model.isSQ;
    if(self.openReloadBlock){
        self.openReloadBlock();
    }
}
- (void)openTPwithArray:(NSArray *)array andview:(UIView *)view andIndex:(int)index{
    [DTKTool openImagAndVideo:array andopenview:view andIndex:index];
}
@end
